package com.woniuxy.factory.service.imp;

import com.woniuxy.factory.entity.Material;
import com.woniuxy.factory.entity.ResponseResult;
import com.woniuxy.factory.exception.BusinessException;
import com.woniuxy.factory.mapper.MaterialMapper;
import com.woniuxy.factory.service.MaterialService;
import com.woniuxy.factory.util.AuditType;
import com.woniuxy.factory.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 邹佳骏
 * @date 2020-08-18 14:46
 */
@Service
public class MaterialServiceImp implements MaterialService {

    @Autowired(required = false)
    MaterialMapper materialMapper;

    @Override
    public List<Material> selectAllMaterials() {

        //查找所有的物料信息
        List<Material> materials = materialMapper.selectAllMaterial();

        return materials;
    }

    @Override
    public ResponseResult addMaterials(Material material) {
        material.setMaeId(UuidUtil.get16UUID());
        int i = materialMapper.insertMaterials(material);
        if (i==0){
            throw new BusinessException("新增失败");
        }
        return new ResponseResult(AuditType.No_Approval,"新增成功",null,null);
    }

    @Override
    public ResponseResult deleteMaterial(String maeId) {
        int i = materialMapper.deleteMaterial(maeId);
        if (i==0){
            throw new BusinessException("删除失败");
        }
        return new ResponseResult(AuditType.No_Approval,"删除成功",null,null);
    }

    @Override
    public ResponseResult geteMaterial(String maeId) {
        Material material = materialMapper.selectMaterialById(maeId);
        if (material==null){
            throw new BusinessException("所要修改的物料信息不存在");
        }
        return new ResponseResult(0,"查询成功",null,material);
    }

    @Override
    public ResponseResult updateMaterial(Material material) {
        int i = materialMapper.updateMaterial(material);
        if (i==0){
            throw new BusinessException("修改失败");
        }
        return new ResponseResult(0,"修改成功",null,null);
    }

}
